﻿Imports System.ComponentModel
Imports System.Drawing

Public Class MySetts


#Region "Properties"

    'ini file (full including .ini ending):
    Private _iniPath As String = Nothing
    Public Property IniPath() As String
        Get
            Return _iniPath
        End Get
        Set(ByVal value As String)
            _iniPath = value
        End Set
    End Property

    Private _ShowWithGE As Boolean = False
    Public Property ShowWithGameEx() As Boolean
        Get
            Return _ShowWithGE
        End Get
        Set(ByVal value As Boolean)
            _ShowWithGE = value
        End Set
    End Property

    Private _ShowDebug As Boolean = False
    Public Property ShowDebugWindow() As Boolean
        Get
            Return _ShowDebug
        End Get
        Set(ByVal value As Boolean)
            _ShowDebug = value
        End Set
    End Property

    Private _KeepLog As Boolean = false
    Public Property KeepLog() As Boolean
        Get
            Return _KeepLog
        End Get
        Set(ByVal value As Boolean)
            _KeepLog = value
        End Set
    End Property

    Private _DebugLog As Boolean = False
    Public Property DebugLog() As Boolean
        Get
            Return _DebugLog
        End Get
        Set(ByVal value As Boolean)
            _DebugLog = value
        End Set
    End Property

    Private _Debug2Log As Boolean = false
    Public Property DebugToLog() As Boolean
        Get
            Return _Debug2Log
        End Get
        Set(ByVal value As Boolean)
            _Debug2Log = value
        End Set
    End Property



#End Region

    'INI FILE:
    Public Ini As New IniFile
    Public converter As TypeConverter = TypeDescriptor.GetConverter(GetType(Font))


    Public Function LoadSetts()

        If _iniPath = Nothing Then Return False

        Try
            Ini.Load(_iniPath)
            '  _GEPath = Ini.GetKeyValue("settings", "GEPath")
            '_dbDir = NotNull(Ini.GetKeyValue("Settings", "DatabaseDir"), "")
            _ShowWithGE = Ini.GetKeyValue("Settings", "ShowWithGE")
            _ShowDebug = Ini.GetKeyValue("Settings", "ShowDebug")
            _DebugLog = Ini.GetKeyValue("Settings", "DebugLog")
            _KeepLog = Ini.GetKeyValue("Settings", "KeepLog")
            _Debug2Log = Ini.GetKeyValue("Settings", "DebugToLog")

            'E.G.s
            '' Saving Font object as a string
            '_font = DirectCast(converter.ConvertFromString(Ini.GetKeyValue("Settings", "MsgFont")), Font)

            '' Saving Colors
            '_msgfgcol = Color.FromArgb(Ini.GetKeyValue("Settings", "MsgFGCol"))
            '_msgbgcol = Color.FromArgb(Ini.GetKeyValue("Settings", "MsgBGCol"))

            Return True

        Catch ex As Exception
            MsgBox(ex.Message & vbCr & vbCr & ex.StackTrace)
        End Try

        Return False
    End Function


    Public Function SaveSetts()
        Ini.SetKeyValue("Settings", "ShowWithGE", _ShowWithGE)
        Ini.SetKeyValue("Settings", "ShowDebug", _ShowDebug)
        Ini.SetKeyValue("Settings", "DebugLog", _DebugLog)
        Ini.SetKeyValue("Settings", "KeepLog", _KeepLog)
        Ini.SetKeyValue("Settings", "DebugToLog", _Debug2Log)


        'Dim converter As TypeConverter = TypeDescriptor.GetConverter(GetType(Font))
        '' Saving Font object as a string
        'Dim fontString As String = converter.ConvertToString(_msgfont)
        'Ini.SetKeyValue("Settings", "MsgFont", fontString)
        '' Saving Colors
        'Ini.SetKeyValue("Settings", "MsgBGCol", _msgbgcol.ToArgb)
        'Ini.SetKeyValue("Settings", "MsgFGCol", _msgfgcol.ToArgb)
        '' Saving Image location
        'Ini.SetKeyValue("Settings", "MsgImg", _msgimg)

        Ini.Save(_iniPath)
        Return False
    End Function

    Public Shared Function NotNull(Of T)(ByVal Value As T, ByVal DefaultValue As T) As T

        If Value Is Nothing OrElse IsDBNull(Value) Then
            Return DefaultValue
        Else
            Return Value
        End If
    End Function

End Class
